<?php

/**
 * test_model
 *
 * Interacts with the users table in the database.
 *
 * @author Michael Bane
 * @link http://code.google.com/
 */

class test_model extends Model {

    /**
    * Name of Function: Constructor
    *
    * Detailed Description: This is my constructor
    * that automatically loads when this class is instantiated
    *
    * @author Michael Bane
    */

    function test_model()
    {
        parent::Model();

        $this->load->database();
    }

    //Functions for my Home Controller

    /**
    * Add User
    *
    * Adds a user to the database.
    *
    * @param string $first_name
    * @param string $last_name
    * @param string $email
    * @param string $username
    * @param string $password
    * @return boolean
    */

    function add_user($first_name, $last_name, $email, $username, $password)
    {

        //Get the values from the user
        $user = array();
        $user['first_name'] = $first_name;
        $user['last_name'] = $last_name;
        $user['email'] = $email;
        $user['username'] = $username;
        $user['password'] = $password;

        //Insert the data into the users table of the database
        $result = $this->db->insert('users', $user);
        return $result;
    }

    /**
     * List Users
     *
     * Returns a list of users from the database.
     *
     * @return array
     */
    
    function list_users()
    {
        $query = $this->db->get('users');  // this line of code is giving me an error

        return $query->result();
    }

    /**
    * Check Credentials
    *
    * Compares the username and password to the equivalent values in the database
    *
    * @param string $username
    * @param string $password
    * @return boolean
    */

    function check_credentials($username, $password)
    {

        $query = $this->db->get_where('users', "`username` = '$username'");  

            if ($query->num_rows() == 1)
            {
                //good we got a user!
                //$password = $this->encrypt->sha1($password);
                $result = $query->row();

                if ($result->password == $password)
                    return $result->userID;
                else
                    return FALSE;
                }
                else
                    return FALSE;
    }

    /**
    * Retrieve User
    *
    * Will retrieve a user from the users table in the database.
    *
    * @param string $user_id
    * @return boolean
    */

    function retrieve_user($user_id)
    {
        //"SELECT * FROM users WHERE `id` = '$user_id'";
        $result = $this->db->get_where('users', array('userID' => $user_id));

        if ($result->num_rows() == 1)
            return $result->row();
        else
            return FALSE;
    }

    //Functions for the User Controller

    /**
    * Add Story
    *
    * Adds a the story submitted by the user to the database
    *
    * @param string $story
    * @return boolean
    */

    function add_story($story)
    {
        $data = array();
        $data['story'] = $story;

        $result = $this->db->insert('data', $data);
        return $result;
    }

    /**
    * Add Image
    *
    * Adds an image submitted by the user to the database
    *
    * @param string $story
    * @return boolean
    */

    function add_image($image)
    {
        $data = array();
        $data['image'] = $image;

        $file_path = $this->db->insert('data', $data);
        return $file_path;
    }

    /**
     * List Stories
     *
     * Returns a list of stories from the database.
     *
     * @return array
     */

    function list_story()
    {
        
        $query = $this->db->get('story');  

        return $query->result();
    }
}

/* EOF */